package com.wltt.product.frfront.config.javafx;

import com.wltt.product.frfront.utils.JsonUtil;
import com.wltt.product.frfront.utils.Md5Util;
import com.wltt.product.frfront.utils.http5.Http5Util;
import com.wltt.product.frfront.utils.http5.HttpMethodEnum;
import com.wltt.product.frfront.utils.http5.HttpResult;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

public class LoginController {

    @FXML
    private Button btnExit;

    @FXML
    private Button btnLogin;

    @FXML
    private TextField tfAccount;
    @FXML
    private PasswordField pfPassword;
    @FXML
    private ProgressIndicator piLoginProgress;
    @FXML
    private Label labelLoginResult;

    /**
     * 点击退出按钮
     *
     * @return
     */
    @FXML
    private void handleExit() {
        System.exit(0);
    }

    /**
     * 点击登录按钮
     *
     * @return
     */
    @FXML
    private void handleLogin() {
        String account = tfAccount.getText();
        String password = pfPassword.getText();

        btnLogin.setDisable(true);
        if (null == account || account.isEmpty()) {
            showTip("请输入账户");
            btnLogin.setDisable(false);
            return;
        }
        if (null == password || password.isEmpty()) {
            showTip("请输入密码");
            btnLogin.setDisable(false);
            return;
        }

        showProgress();
        Platform.runLater(() -> {
            try {
                Map<String, String> body = new HashMap<>();
                body.put("userAccount", account);
                body.put("userPassword", password);
                HttpResult httpResult = Http5Util.doCallJson(HttpMethodEnum.POST, "http://localhost:8090/public/login", JsonUtil.toJson(body));
                if (HttpStatus.OK.value() != httpResult.getHttpCode()) {
                    if (null != httpResult.getBody()) {
                        String message = httpResult.getBody();
                        showTip(JsonUtil.getJsonNode(message, "message").asText());
                    } else {
                        showTip("服务器拒绝登录");
                    }
                    return;
                }
                loginSuccess();
            } catch (Exception e) {
                showTip("登录失败,无法请求服务器");
            } finally {
                piLoginProgress.setVisible(false);
                btnLogin.setDisable(false);
            }
        });
    }

    /**
     * 登录成功后跳转主界面
     *
     * @return
     */
    private void loginSuccess() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(new ClassPathResource("javafx/main-windows.fxml").getURL());
            Stage stage = new Stage();
            Image icon = new Image(new ClassPathResource("icon/logo.jpg").getInputStream());
            stage.getIcons().add(icon);
            Scene scene = new Scene(fxmlLoader.load(), 1150, 750);
            scene.getStylesheets().addAll("css/style.css");
            stage.setScene(scene);
            stage.setTitle("通驰焊接机器人软件 v3.8.5");
//            stage.initStyle(StageStyle.UNDECORATED);
//            stage.setFullScreen(true);
            stage.setOnCloseRequest(event -> {
                System.exit(0);
            });
            stage.setMaximized(true);
            stage.show();
            ((Stage) btnLogin.getScene().getWindow()).close();
        } catch (Exception e) {
            e.printStackTrace();
            showTip("客户端异常");
        } finally {
            piLoginProgress.setVisible(false);
        }
    }

    /**
     * 显示登录提示
     *
     * @return
     */
    private void showTip(String tip) {
        piLoginProgress.setVisible(false);
        labelLoginResult.setText(tip);
        labelLoginResult.setVisible(true);
    }

    /**
     * 显示登录进度
     *
     * @return
     */
    private void showProgress() {
        piLoginProgress.setVisible(true);
        labelLoginResult.setText("");
    }
}